package com.shycloud.mido.organ.test.util;


import java.sql.*;

/**
 * @author Xiaoxing Yu
 * @date 2020/5/29
 **/
public class DBUtil {

	static{
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//加载数据库驱动
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private DBUtil() {}

	public static Connection getConn(String databaseName){
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(
					"jdbc:mysql://119.3.167." +
							"101:3306/" + databaseName + "?serverTimezone=GMT%2B8", "root","Joy@jykj123456");//连接数据库
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}

	public static void closeConn(Connection conn){
		try {
			if(conn != null ){
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Statement getStmt(Connection conn){
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stmt;
	}

	public static PreparedStatement getPStmt(Connection conn,String sql){
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pstmt;
	}

	public static void closeStmt(Statement stmt){
		try {
			if(stmt != null ){
				stmt.close();
				stmt = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static ResultSet executeQuery(Statement stmt,String sql){
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public static void closeRs(ResultSet rs){
		try {
			if(rs != null ){
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static ResultSet executeQuery(Connection conn,String sql){//重载
		ResultSet rs = null;
		try {
			rs = conn.createStatement().executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

}
